var gulp = require('gulp');
var uglify = require('gulp-uglify');
var rename = require("gulp-rename");
var minifyCss = require('gulp-minify-css');
var rev = require('gulp-rev');
var concat = require('gulp-concat');

var js_src = 'src/js/';
var css_src = 'src/css/';

var js_dest = '../static/js/';
var css_dest = '../static/css/';

var css_task = {
    'mainPage': ['uikit.almost-flat.css', 'markdown.css', 'pygments.css','main-page.css'],
    'listPage': ['uikit.almost-flat.css', 'markdown.css', 'list-page.css'],
    'TreeStyle': ['TreeStyle.css'],
    'confPage': ['uikit.almost-flat.css','notify.css']
};
var js_task = {
    'mainPage': ['jquery-2.2.0.js', 'uikit.js','lightbox.js','sticky.js', 'mainPage.js'],
    'listPage': ['jquery-2.2.0.js', 'uikit.js', 'listPage.js'],
    'confPage': ['jquery-2.2.0.js', 'uikit.js', 'notify.js', 'conf-page.js'],
    'TreeJS': ['list-treePage.js']
};


function task_creator(type, src, dest, task) {
    var dict={
        'css':['.css',minifyCss],
        'js':['.js',uglify]
    };
    return function () {
        for (var name in task) {
            console.log(name);
            var list = [];
            for (var i in task[name]) {
                list.push(src + task[name][i])
            }
            console.log(list);
            gulp.src(list)
                .pipe(concat(name))
                .pipe(dict[type][1]())
                .pipe(rename({
                    basename: name,
                    suffix: ".min",
                    extname: dict[type][0]
                }))
                .pipe(gulp.dest(dest));
        }
    }
}

gulp.task('minCSS', task_creator('css', css_src, css_dest, css_task));
gulp.task('minJS', task_creator('js', js_src, js_dest, js_task));

gulp.task('default', ['minCSS', 'minJS']);

var watch_js = gulp.watch(js_src+'*.js', ['minJS']);
watch_js.on('change', function(event) {
  console.log('File ' + event.path + ' was ' + event.type + ', running tasks...');
});
var watch_css = gulp.watch(css_src+'*.css', ['minCSS']);
watch_css.on('change', function (event) {
    console.log('File ' + event.path + ' was ' + event.type + ', running tasks...');
});